<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
  <%-- <%@include file="../../common/taglib.jsp"%> --%>
  
      <div class="row">
        <div class="col-xs-12">
          <div class="box">
            <div class="box-header">
              <h3 class="box-title">
              </h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
            <div class="clearfix">
					<div class="col-md-4">
						<div class="input-group date ">
							<div class="input-group-addon">
								<i class="fa fa-calendar"></i>
							</div>
							<input type="text" class="form-control pull-right" id="beginDateTrade" placeholder="开始时间...">
							<input type="text" class="form-control pull-right" id="endDateTrade" placeholder="结束时间...">
						</div>
					</div>
					<div class="col-md-4">
						<div class="input-group">
							<span class="input-group-addon"><i class="fa fa-search"></i></span>
							<input type="text" class="form-control" id="merchantOrderNoTrade" placeholder="商户订单号...">
							<select class="form-control" id="statusTrade">
								<option value="">选择订单状态</option>
								<option value="SUCCESS">交易成功</option>
								<option value="FINISH">交易完成</option>
								<option value="WAITING_PAYMENT">等待支付</option>
							</select>
							<select class="form-control" id="payWayName">
								<option value="">选择支付方式</option>
								<option value="WEIXIN">微信</option>
								<option value="ALIPAY">支付宝</option>
								<option value="QUICKPAY">快捷支付</option>
								<option value="DEDUCTPAY">代扣支付</option>
								<option value="B2CPAY">B2C支付</option>
							</select>
						</div>
					</div>
					<div class="col-md-4">
						<button type="submit" id="searchTrade" class="btn btn-primary">搜索</button>
						<button type="submit" id="exportTrade" class="btn btn-primary">导出</button>
					</div>
				</div>
				<div>&nbsp;&nbsp;订单总金额：<font color="red" id="orderAmountTH"></font>&nbsp;&nbsp;&nbsp;&nbsp;订单总手续费：<font color="red" id="platIncomeTH"></font></div>
              <table id="pageTableTrade" class="table table-bordered table-hover">
                <thead>
                <tr>
                  <th>商户订单号</th>
                  <th>支付流水号</th>
                  <th>业务类型</th>
                  <th>支付方式</th>
                  <th>订单金额</th>
                  <th>订单手续费</th>
                  <th>状态</th>
                  <th>创建时间</th>
                  <th>操作</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
        <!-- /.col -->
      </div>
     <!-- DataTables -->
<script src="${baseURL }/lte/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="${baseURL }/lte/plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- page script -->
<script>
$("#searchTrade").on("click",function(){
	searchTrade();
	sumAmountTrade();
});

//初始化时间选择器
$('#beginDateTrade').datepicker({
	autoclose: true,
	defaultDate:'-1M',
	beforeShowDay: $.noop,
	calendarWeeks: false,
	clearBtn: false,
	daysOfWeekDisabled: [],
	endDate: Infinity,
	forceParse: true,
	format: 'yyyy-mm-dd',
	keyboardNavigation: true,
	language: 'cn',
	minViewMode: 0,
	multidate: false,
	multidateSeparator: ',',
	orientation: "auto",
	rtl: false,
	startDate: -Infinity,
	startView: 0,
	todayBtn: false,
	todayHighlight: false,
	weekStart: 0
});

$('#endDateTrade').datepicker({
	autoclose: true,
	defaultDate:'-1M',
	beforeShowDay: $.noop,
	calendarWeeks: false,
	clearBtn: false,
	daysOfWeekDisabled: [],
	endDate: Infinity,
	forceParse: true,
	format: 'yyyy-mm-dd',
	keyboardNavigation: true,
	language: 'cn',
	minViewMode: 0,
	multidate: false,
	multidateSeparator: ',',
	orientation: "auto",
	rtl: false,
	startDate: -Infinity,
	startView: 0,
	todayBtn: false,
	todayHighlight: false,
	weekStart: 0
});

function retrieveData( sSource, aoData, fnCallback ) {      
	aoData.push({ "name": "beginDate", "value": $("#beginDateTrade").val()},{ "name": "endDate", "value": $("#endDateTrade").val()},
			{ "name": "merchantOrderNo", "value": $("#merchantOrderNoTrade").val()},{ "name": "status", "value": $("#statusTrade").val()},{ "name": "payWayName", "value": $("#payWayName").val()}); 
    $.ajax( {     
        "type": "POST",      
        "contentType": "application/json",     
        "url": sSource,      
        "dataType": "json",     
        "data": JSON.stringify(aoData), //以json格式传递     
        "success": function(resp) {     
            fnCallback(resp); //服务器端返回的对象的returnObject部分是要求的格式    
        }     
    });      
}    

var oTableTrade = null;     

$(function() {     
    searchTrade();
    sumAmountTrade();
} );     
    
//“检索”按钮的处理函数     
function searchTrade() {     
    if (oTableTrade == null) { //仅第一次检索时初始化Datatable     
        $("#pageTableTrade").show();     
        oTableTrade = $('#pageTableTrade').dataTable( {     
            "bAutoWidth": false,                    //不自动计算列宽度     
            "aoColumns": [                          //设定各列宽度     
                            {"mData" : "merchantOrderNo"},
                            {"mData" : "trxNo"},
                            {"mData" : "trxTypeDesc"},
                            {"mData" : "payWayNameDesc"},
                            {"mData" : "orderAmount"},
                            {"mData" : "platIncome"},
                            {"mData" : "statusDesc"},
                            {"mData" : "createTimeDesc"},
                            {"mData" : null}
                        ],
                        "aoColumnDefs":[{
                       	 "targets" : -1,
               				"mData" : null,
               				"render" : function(mData) {
               					if(mData.status == "SUCCESS" ||mData.status == "FINISH"){
	               					return '<button onclick="sendOrderMsg(\''+mData.merchantOrderNo+'\')" class="btn btn-default btn-xs">通知</button>';
               					}
               					return '';
               				}
                        }],
            "bProcessing": true,                    //加载数据时显示正在加载信息     
            "bServerSide": true,                    //指定从服务器端获取数据     
            "bFilter": false,                       //不使用过滤功能     
            "bLengthChange": false,                 //用户不可改变每页显示数量     
            "iDisplayLength": 10,                    //每页显示10条数据     
            "sAjaxSource": "${baseURL }/merchant/trade/ajaxPaymentList",//获取数据的url     
            "fnServerData": retrieveData,           //获取数据的处理函数     
            "sPaginationType": "full",      //翻页界面类型     
            "searching": false,
            "ordering": false,
            "oLanguage": {                          //汉化     
                "sZeroRecords": "没有检索到数据",     
                "sInfo": "总共有 _TOTAL_ 条记录",     
                "sInfoEmtpy": "没有数据",     
                "sProcessing": "正在加载数据...",     
                "oPaginate": {     
                    "sFirst": "首页",     
                    "sPrevious": "前一页",     
                    "sNext": "后一页",     
                    "sLast": "尾页"    
                }     
            }     
        });     
    }     
    
    //刷新Datatable，会自动激发retrieveData     
    oTableTrade.fnDraw();     
}

function sendOrderMsg(merchantOrderNo){
	$.ajax({
		type : "POST",
		data : {
			"merchantOrderNo" : merchantOrderNo
		},
		dataType : "json",
		url: "${baseURL }/merchant/trade/order/msg",
		 //请求成功完成后要执行的方法  
        success: function(data) {
        	alertMsg("成功发起通知","success");
        },  
        error : function(data) {
        	alertMsg("通知失败","danger");
        } 
	});
}

function sumAmountTrade(){
	$("#orderAmountTH").html("");
	$("#platIncomeTH").html("");
	var beginDate = $("#beginDateTrade").val();
	var endDate = $("#endDateTrade").val();
	var merchantOrderNo = $("#merchantOrderNoTrade").val();
	var status = $("#statusTrade").val();
	var payWayName = $("#payWayName").val();
	$.ajax({  
        type: "GET",
        data : {
        	beginDate : beginDate,
        	endDate : endDate,
        	merchantOrderNo : merchantOrderNo,
        	status : status,
        	payWayName : payWayName
		},
        dataType:'json',
        url: "${baseURL }/merchant/trade/sumAmountByMerchantNo",
        //请求成功完成后要执行的方法  
        success: function(result){
        	$("#orderAmountTH").html(result.sumOrderAmount);
        	$("#platIncomeTH").html(result.sumPlatIncome);
        },  
        error : function() {
        	 
        }   
    });
}

$("#exportTrade").on("click",function(){
	exportReportTrade();
});

function exportReportTrade(){
	var beginDate = $("#beginDateTrade").val();
	var endDate = $("#endDateTrade").val();
	var merchantOrderNo = $("#merchantOrderNoTrade").val();
	var status = $("#statusTrade").val();
	var payWayName = $("#payWayName").val();
	window.location.href = "${baseURL }/merchant/trade/listPaymentRecordExportExecl?beginDate=" + beginDate + "&endDate=" + endDate + 
	  "&merchantOrderNo=" + merchantOrderNo + "&status=" + status + "&payWayName=" + payWayName;
}
</script>